/**   
* @Title: UsbkeyDao.java
* @Package gov.pbc.gx.csm.usbkey.dao
* @Description: TODO(用一句话描述该文件做什么)
* @author WLF
* @date 2015-12-23 下午10:23:08
* @version V1.0
*/ 
package gov.pbc.nn.kjgl.maintenance.usb.dao;

import gov.pbc.nn.kjgl.maintenance.usb.entity.CommonUserUsbkey;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

/** 
 * @ClassName: UsbkeyDao
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author WLF
 * @date 2015-12-23 下午10:23:08
 * 
 */
@Component(UsbkeyDao.bean_id)
public class UsbkeyDao extends HibernateDao {
	public final static String bean_id="usbkeyDao";
	
	public void saveUk(CommonUserUsbkey uk){
		super.getSessionFactory().getCurrentSession().save(uk);
	}
	
	public void updateUk(CommonUserUsbkey uk){
		super.getSessionFactory().getCurrentSession().update(uk);
	}
	
	public void deleteUk(CommonUserUsbkey uk){
		super.getSessionFactory().getCurrentSession().delete(uk);
	}
	
	public CommonUserUsbkey loadSaById(int id){
		return (CommonUserUsbkey)super.getSession().get(CommonUserUsbkey.class,id);
	}
	public void getAllUkPage(Page<CommonUserUsbkey> page,Criteria criteria) throws Exception{		
			DetachedCriteria detachedCriteria=this.buildDetachedCriteria(criteria,CommonUserUsbkey.class);
			detachedCriteria.addOrder(Order.desc("id"));
			this.pagingQuery(page,detachedCriteria);

	}
	
	public String queryUsbkeyByUsername(String username){
		String certIdsString="";
		 Map<String, Object> map=new HashMap<String,Object>();
		  if(username!=null||!"".equals(username))
	        {
	            String sql="select uk from CommonUserUsbkey uk";
	            map.put("username", username);
	            sql+=" where uk.username=:username";
	            List<CommonUserUsbkey> uks=this.query(sql, map);
	            return uks.get(0).getCertId().toString();
	        }else{
	            return "";
	        }
	 }
	
	
	public boolean hasCertId(String certId){
	    Map<String,Object> map =new HashMap<String,Object>();
	    map.put("certId", certId);
	    String sql="select count(uk) from CommonUserUsbkey uk where uk.certId=:certId";
	    int count=this.queryForInt(sql, map);
	    if(count>0){
	        return false;
	    }
	    return true;
	}
}
